<?php

namespace App\Http\Controllers\Frontend;


use App\Logics\EmailLogic;
use App\Logics\SiteLogic;
use App\Service\Curl;


class EmailController extends BaseController
{
    // 邮件订阅 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 首页
    public function index()
    {
        /*// 如果是提交表单
        $method = $this->request->method();
        if($method == 'POST'){
            $logic = new SIteLogic();
            $res   = $logic->emailSubscripte();
            if($res === false){
                return $this->error($logic->getError());
            }

            return $this->success([], '邮件订阅操作成功！');
        }*/

        return $this->dispaly('frontend.email');
    }

    // 订阅
    //  email
    public function ding()
    {
        $logic = new SiteLogic();
        $res   = $logic->emailSubscripte();
        if($res === false){
            return $this->error($logic->getError());
        }

        /*// 同步倒 北美商业电讯
        //  当前网站链接  https://asiatechwire.com/email-ding.html
        try{
            $option = $this->optLogic();
            $email  = $option->getData('email', null, 'trim');
            $url    = '';
            $curl   = new Curl();
            $curl->post(['email' => $email])->request($url);
        } catch(\Exception $e){
        }*/

        return $this->success([], '邮件订阅操作成功！');
    }

    // 取消订阅邮件
    public function cancel()
    {
        $logic = new SiteLogic();
        $res   = $logic->emailCancel();
        if($res === false){
            return $this->error($logic->getError());
        }

        /*// 同步倒 北美商业电讯
        //  当前网站链接  https://asiatechwire.com/email-cancel.html
        try{
            $option = $this->optLogic();
            $email  = $option->getData('email', null, 'trim');
            $url    = '';
            $curl   = new Curl();
            $curl->post(['email' => $email])->request($url);
        } catch(\Exception $e){
        }*/

        return $this->success([], '取消邮件订阅成功！');
    }


    // 邮件列表
    public function lists()
    {
        // 数据
        $html     = $this->getLists();
        $nextPage = $this->nextPage('email-more');  // 下一页

        // 传值
        $option = $this->optLogic();
        $option->assign('nextPage', $nextPage);
        $option->assign('html', $html);

        return $this->dispaly('frontend.email.lists');
    }

    // 获取更多
    public function more()
    {
        $html     = $this->getLists();
        $nextPage = $this->nextPage('email-more');  // 下一页

        return $this->success(['html' => (string)$html, 'nextUrl' => $nextPage]);
    }

    // 获取列表
    protected function getLists()
    {
        $option = $this->optLogic();
        $logic  = new EmailLogic();
        $result = $logic->onLists();

        // 结果整理
        $result['style_host'] = env('STYLE_HOST');
        $result['app_host']   = request()->getSchemeAndHttpHost().'/';

        return $this->dispaly('frontend.email.on-list', $result);
    }
}


